<template>
  <div class="md-user-order-info">
    <div class="r-title">
      <span>Order Information</span>
    </div>
    <div class="user-info-main">
      <div class="main-box">
        <el-table :data="tableData">
          <el-table-column prop="productPicture" label="product picture" min-width="150">
            <template slot-scope="scope">
              <img :src="scope.row.productPicture ? JSON.parse(scope.row.productPicture)[0] : ''" :alt="scope.row.productTitle" style="width: 120px;" />
            </template>
          </el-table-column>
          <el-table-column prop="productTitle" label="product name" min-width="120"></el-table-column>
          <el-table-column prop="status" label="status" min-width="120">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.status == '0'" type="success">Success</el-tag>
              <el-tag v-if="scope.row.status == '1'" type="info">Disable</el-tag>
              <el-tag v-if="scope.row.status == '2'" type="warning">Approving</el-tag>
              <el-tag v-if="scope.row.status == '3'" type="danger">Failed</el-tag>
              <el-tag v-if="scope.row.status == '4'" type="warning">Waiting Delivery</el-tag>
              <el-tag v-if="scope.row.status == '5'" type="warning">Waiting Receipt</el-tag>
              <el-tag v-if="scope.row.status == '6'">Done</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="operate" label="operate" min-width="120">
            <template slot-scope="scope">
              <!-- 待发货 选择收货地址 -->
              <el-button type="text" v-if="scope.row.status == '4'" @click="handleSelect(scope.row)">Select Address</el-button>
              <el-button type="text" v-else-if="scope.row.status == '5'" @click="handleEditLink(scope.row)">Product Link</el-button>
              <span v-else>completed</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <!-- 选择地址 -->
    <md-address-select-dialog :flag="flag" @close="flag = false" @confirm="selectAddress"></md-address-select-dialog>
    <!-- 填写产体验地址 -->
    <el-dialog title="Product Experience Address" width="500px" :visible.sync="dialogFlag" append-to-body :close-on-click-modal="false" :close-on-press-escape="false" @close="dialogFlag = false" center>
      <el-form style="margin-top: -20px" ref="form" :model="form" :rules="rules">
        <el-form-item prop="callbackUrl" label="product link address:">
          <el-input type="textarea" v-model="form.callbackUrl" placeholder="Please input link address" :autosize="{ minRows: 4, maxRows: 4}" />
        </el-form-item>
        <el-form-item>
          <div class="btn-box">
            <el-button type="primary" style="width: 170px;" @click="handleSubmit()">Confirm</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: 'MDOrderBaseInfo',
  layout: 'dynamic',
  data () {
    return {
      tableData: [],
      flag: false,
      rowInfo: {},
      dialogFlag: false,
      form: {
        callbackUrl: ''
      },
      rules: {
        callbackUrl: [{ required: true, message: 'This field is required!', trigger: 'blur' }]
      }
    }
  },
  mounted () {
    this.getOrderList()
  },
  methods: {
    async getOrderList () {
      const options = {
        userId: this.$local.getItem('user').userId,
        pageIndex: 1,
        pageSize: 100
      }
      const res = await this.$axios.post('web/productOrder/listPage', options)
      if (res.code == 200) {
        this.tableData = res.data && res.data.items
      }
    },
    // 编辑产品体验地址
    handleEditLink (row) {
      this.dialogFlag = true
      this.rowInfo = row
    },
    handleSubmit () {
      this.$refs.form.validate(valid => {
        if (valid) {
          const options = {
            orderId: this.rowInfo.orderId,
            callbackUrl: this.form.callbackUrl,
            status: '6'
          }
          this.updateOrderInfo(options, () => { this.dialogFlag = false })
        }
      })
    },
    // 选择收货地址
    handleSelect (row) {
      this.flag = true
      this.rowInfo = row
    },
    selectAddress (obj) {
      const options = {
        orderId: this.rowInfo.orderId,
        addressId: obj.addressId,
        status: '5'
      }
      this.updateOrderInfo(options)
    },
    async updateOrderInfo (options, callback) {
      const res = await this.$axios.put('web/productOrder', options)
      if (res.code == 200) {
        this.$message.success('Update success!')
        this.getOrderList()
        callback && callback()
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.md-user-order-info {
  width: 100%;
  .r-title {
    height: 40px;
    line-height: 40px;
    border-bottom: 1px solid #ededed;
    display: flex;
    align-items: center;
    justify-content: space-between;
    color: #414141;
    padding-right: 20px;
    box-sizing: border-box;
    span {
      font-weight: 100;
      font-size: 24px;
      line-height: 40px;
      border-bottom: 1px solid $md-primary-color;
    }
  }
  .user-info-main {
    margin-top: 20px;
    .content {
      margin-top: 20px;
      .order_header {
        height: 40px;
        background: #f4f6f9;
        font-size: 16px;
        color: #1d2129;
        line-height: 22px;
        padding: 9px 19px;
        display: flex;
        .order_header_1 {
          margin-right: 230px;
        }
        .order_header_2 {
          span {
            margin-left: 70px;
          }
        }
      }
      .item {
        cursor: pointer;
        border: 1px solid #e5e6eb;
        margin-top: 15px;
        .item_top {
          background: #f4f6f9;
          font-size: 14px;
          color: #86909c;
          line-height: 30px;
          padding-left: 19px;
          span {
            margin-right: 13px;
          }
        }
        .main {
          padding: 14px 19px;
          display: flex;
          align-items: center;
          justify-content: space-between;
          .main_left {
            display: flex;
            align-items: center;
            dl {
              display: flex;
              dt {
                img {
                  width: 157px;
                  height: 80px;
                  border-radius: 4px;
                }
                margin-right: 16px;
              }
              dd {
                div {
                  &:first-child {
                    font-size: 16px;
                    color: #1d2129;
                    line-height: 22px;
                    margin-top: 10px;
                  }
                  &:last-child {
                    font-size: 14px;
                    color: #86909c;
                    line-height: 20px;
                    margin-top: 12px;
                  }
                }
              }
            }
            .left_1 {
              margin-left: 43px;
              font-size: 14px;
              line-height: 20px;
              span {
                &:first-child {
                  color: #1d2129;
                }
                &:nth-child(2) {
                  color: #ff7376;
                  margin-left: 112px;
                }
                &:last-child {
                  margin-left: 96px;
                  color: #1d2129;
                  i {
                    width: 8px;
                    height: 8px;
                    background: #4cb052;
                    display: inline-block;
                    border-radius: 50%;
                    margin-right: 4px;
                  }
                }
              }
            }
          }
          .main_right {
            .el-button {
              border: 1px solid #2470dc;
              color: #2470dc;
            }
          }
        }
      }
    }
  }
}
.btn-box {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  margin-bottom: -10px;
}
</style>